package lanou.giftsay_01.fragment.main;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.bmob.v3.BmobUser;
import lanou.giftsay_01.tools.GiftUser;
import lanou.giftsay_01.adapter.MineAdapter;
import lanou.giftsay_01.fragment.mine.SecondFragment;
import lanou.giftsay_01.fragment.mine.SecondRightFragment;
import lanou.giftsay_01.main.LogInActivity;
import lanou.giftsay_01.main.LogOutActivity;
import lanou.giftsay_01.tools.BaseFragment;
import lanou.giftsay_01.R;
import lanou.giftsay_01.tools.MyCircleimageView;

/**
 * Created by dllo on 16/8/15.
 */
public class MineFragment extends BaseFragment {
    private TabLayout tb;
    private ViewPager viewPager;
    private TextView textView;
    private MyCircleimageView myCircleimageView;
    private ArrayList<Fragment> fragments;
    private MineAdapter mineAdapter;
    private RelativeLayout contentFl;
    private ImageView options;

    @Override
    protected int setLayout() {
        return R.layout.mine_fragment;
    }
    @Override
    protected void initView() {
        tb = (TabLayout) getView().findViewById(R.id.tb_mine);
        viewPager = (ViewPager) getView().findViewById(R.id.vp_mine);
        myCircleimageView = (MyCircleimageView) getView().findViewById(R.id.ic_mine);
        textView = (TextView) getView().findViewById(R.id.name_up_tv);
        contentFl = (RelativeLayout) getView().findViewById(R.id.content_fl);
        options = (ImageView) getView().findViewById(R.id.shezhi_iv);
    }
    @Override
    protected void initData() {
        /**
         * 设置的点击事件
         * 点击它来设置图片
         */
        options.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           Intent intent = new Intent(Intent.ACTION_PICK , android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(intent , 1);
            }
        });
        /**
         * 我的布局
         */
        mineAdapter = new MineAdapter(getChildFragmentManager());
        fragments = new ArrayList<>();
        ArrayList<String> name = new ArrayList<>();
        name.add("单品");
        name.add("攻略");
        mineAdapter.setName(name);
        ArrayList<Fragment> fragment = new ArrayList<>();
        fragment.add(new SecondFragment());
        fragment.add(new SecondRightFragment());
        mineAdapter.setFragments(fragment);
        viewPager.setAdapter(mineAdapter);
        tb.setupWithViewPager(viewPager);
        Log.d("MineFragment", "aaa");
        /**
         * 头像的点击事件
         */
        myCircleimageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GiftUser giftUser =   BmobUser.getCurrentUser(GiftUser.class);
                if (giftUser == null) {
                  Intent intent = new Intent(MineFragment.this.getContext(), LogInActivity.class);
                  startActivity(intent);
                }else {
                  Intent intent = new Intent(MineFragment.this.getContext(), LogOutActivity.class);
                  startActivity(intent);
                }
            }
        });

    }

    /**
     * 判断是否登录
     * 如果登录了就变换布局
     */
    @Override
    public void onResume() {
        super.onResume();
        GiftUser giftUser = BmobUser.getCurrentUser(GiftUser.class);
        if (giftUser != null){
            if (giftUser.getIcon()!= null) {
                myCircleimageView.setImageBitmap(giftUser.getIcon());
            }else {
                myCircleimageView.setImageResource(R.mipmap.me_avatar_boy);

            }
            textView.setText(giftUser.getUsername());
            contentFl.setVisibility(View.GONE);
            viewPager.setVisibility(View.VISIBLE);
        }else {
            contentFl.setVisibility(View.VISIBLE);
            viewPager.setVisibility(View.GONE);
            myCircleimageView.setImageResource(R.mipmap.me_avatar_boy);
            textView.setText("请登录");
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == getActivity().RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
            myCircleimageView.setImageBitmap(bitmap);
            // String picturePath contains the path of selected Image
        }

//    /**
//     *
//     * @param uri
//     */
//    public void cropPhoto(Uri uri) {
//        Intent intent = new Intent("com.android.camera.action.CROP");
//        intent.setDataAndType(uri, "image/*");
//        intent.putExtra("crop", "true");
//        // aspectX aspectY 是宽高的比例
//        intent.putExtra("aspectX", 1);
//        intent.putExtra("aspectY", 1);
//        // outputX outputY 是裁剪图片宽高
//        intent.putExtra("outputX", 150);
//        intent.putExtra("outputY", 150);
//        intent.putExtra("return-data", true);
//        startActivityForResult(intent, 3);
    }

}
